package org.tio.sitexxx.im.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.TioClient;
import org.tio.client.intf.ClientAioHandler;
import org.tio.client.intf.ClientAioListener;
import org.tio.sitexxx.im.client.handler.ImClientHandler;
import org.tio.sitexxx.im.client.ui.JFrameMain;
import org.tio.sitexxx.im.common.ImTioUuid;
import org.tio.sitexxx.service.init.CacheInit;
import org.tio.sitexxx.service.init.PropInit;
import org.tio.sitexxx.service.init.RedisInit;
import org.tio.utils.jfinal.P;

/**
 * @author tanyaowu
 *
 */
public class TioSiteImClientStarter {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(TioSiteImClientStarter.class);

	private static ReconnConf reconnConf = new ReconnConf(5000L);

	private ClientAioHandler	tioClientHandler	= new TioSiteImClientAioHandler(new PacketDispatcher(new String[] { ImClientHandler.class.getPackage().getName() }));
	private ClientAioListener	aioListener			= new TioSiteImClientAioListener();
	private ClientGroupContext	clientGroupContext	= new ClientGroupContext(tioClientHandler, aioListener, reconnConf);
	private TioClient			tioClient			= null;

	public TioSiteImClientStarter() throws Exception {
		long workerId = P.getLong("uuid.workerid");
		long datacenterId = P.getLong("uuid.datacenter");
		ImTioUuid imTioUuid = new ImTioUuid(workerId, datacenterId);
		clientGroupContext.setTioUuid(imTioUuid);

		clientGroupContext.useSsl();

		tioClient = new TioClient(clientGroupContext);
	}

	/**
	 * @return the tioClient
	 */
	public TioClient getTioClient() {
		return tioClient;
	}

	/**
	 * @return the clientGroupContext
	 */
	public ClientGroupContext getClientGroupContext() {
		return clientGroupContext;
	}

	public static void main(String[] args) throws Exception {
		PropInit.init();

		RedisInit.init();

		CacheInit.init();

		JFrameMain.main(args);
	}
}
